package com.example.login;

import com.example.home.BEnumRespond;
import com.example.home.BRespond;
import com.example.home.StringEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
@RestController
@RequestMapping(value = "login/")
//@ResponseStatus(HttpStatus.OK)
public class LoginController {
    @PostMapping("pwd")
    public Object login_pwd(LoginPwd login){
        String account = login.getAccount();
        String pwd = login.getAccount();
        if (StringEmpty.isNULL(account) || StringEmpty.isNULL(pwd)){
            return  BRespond.respond_failure(BEnumRespond.noparam);
        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("age",40);
        map.put("name", "吕布");
        map.put("phone","15160023363");
        map.put("account",account);
        map.put("pwd",pwd);
        return BRespond.respond_success(map);
    }
   @PostMapping("code")
    public Object login_code(LoginCode loginCode){
        String phone = loginCode.getPhone();
        String code = loginCode.getCode();
        if (StringEmpty.isNULL(code) || StringEmpty.isNULL(phone)){
            return BRespond.respond_failure(BEnumRespond.noparam);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("age",40);
        map.put("name", "吕布");
        map.put("phone",phone);
        map.put("code",code);
        return BRespond.respond_success(map);
    }
    @PostMapping(value = "token")
    //直接参数方式
//    public Object login_token(String token){
//        return get_data(token);
//    }
//    //map参数方式
//    public Object login_token(@RequestParam Map<String,Object> map){
//        String token = map.getOrDefault("token","").toString();
//        return get_data(token);
//    }
    public Object login_token(LoginToken loginToken){
        String token = loginToken.token;
        return get_data(token);
    }
    //模型参数方式
    public  Object get_data(String token){
        if (StringEmpty.isNULL(token)){
            return BRespond.respond_failure(BEnumRespond.noparam);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("age",40);
        map.put("name","吕布");
        map.put("token",token);
        return BRespond.respond_success(map);
    }
    @PostMapping("upload")
    public Object upload_file(@RequestParam("file") MultipartFile my_file){
        if (my_file == null) {
            throw new RuntimeException("文件不能为空");
        }
        if (my_file.getSize() >= 20 * 1024 * 1024) {
            throw new RuntimeException("文件大小超出最大限制");
        }
        byte[] bytes = null;
        try {
            bytes = my_file.getBytes();
            System.out.println(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "type："+my_file.getContentType()
                +" fileName："+my_file.getOriginalFilename()
                +" size："+my_file.getSize();
    }
}
